/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

/**
 *
 * @author Barbar
 */

import java.net.*;
import java.io.*;
import services.Command;


public class transferfileClient
{
    Socket ClientSoc;
    DataInputStream din;
    DataOutputStream dout;
    BufferedReader br;
    Command.Action action;
    String filename;
   
transferfileClient(Socket soc, Command.Action ac, String s)
    {
        this.action = ac;
        this.filename = s;
        try
        {
            ClientSoc=soc;
            ClientSoc.setSoTimeout(10000);
            din=new DataInputStream(ClientSoc.getInputStream());
            dout=new DataOutputStream(ClientSoc.getOutputStream());
            br=new BufferedReader(new InputStreamReader(System.in));
         
        }
        catch(Exception ex)
        {
        }        
    }
    void SendFile() throws Exception
    {        
 if(!ClientSoc.isClosed()){
        File f=new File(filename);
        if(!f.exists())
        {
            System.out.println("File not Exists...");
            dout.writeUTF("File not found");
           
        }
       
        dout.writeUTF(filename);
       
     
     
        System.out.println("Sending File ...");
        FileInputStream fin=new FileInputStream(f);
       
        int ch;
        do
        {
           
            ch=fin.read();
            dout.writeUTF(String.valueOf(ch));
        }
        while(ch!=-1);
         
        fin.close();
       
        System.out.println(din.readUTF());
         System.out.println("File send");
        return;
 }
  else{ System.out.println(getClass().getSimpleName() + "Zamknięto socket klienta - brak mozliwosci wysylu danych");
      return;
      }
       
    }
   
    void ReceiveFile() throws Exception
    {
       
     if(!ClientSoc.isClosed()){  
        dout.writeUTF(filename);
        String msgFromServer=din.readUTF();
       
        if(msgFromServer.compareTo("File Not Found")==0)
        {
            System.out.println("File not found on Server ...");
            return;
        }
        else if(msgFromServer.compareTo("READY")==0)
        {
            System.out.println("Rozpoczynanie pobierania pliku");
            File f=new File(filename);
           
            FileOutputStream fout=new FileOutputStream(f);
            int ch;
            String temp;
            do
            {
                temp=din.readUTF();
                ch=Integer.parseInt(temp);
                if(ch!=-1)
                {
                    fout.write(ch);                    
                }
            }while(ch!=-1);
            fout.close();
            System.out.println(din.readUTF());
            System.out.println("Zakończono odbieranie danych");
            return;
               
        }
     }
      else{ System.out.println(getClass().getSimpleName() + "Zamknięto socket klienta - brak mozliwosci wysylu danych");
     
      return;
      }
       
       
    }

    public void displayMenu() throws Exception
    {
        while(true)
        {    
           
            int choice;
            choice=this.action.getAction();
            if(choice==1)
            {
                dout.writeUTF("PUT");
                SendFile();
               
                return;
           //     System.exit(0);
            }
            else if(choice==2)
            {
                dout.writeUTF("GET");
                ReceiveFile();
                return;
            }
            else
            {
                dout.writeUTF("DISCONNECT");
                return;
               
            }
        }
    }
}
